
<style type="text/css">
	#iframe_pay{
		border: none;
	}
	.step_footer{
		display: none !important;
	}
</style>
<link href="https://ceshi-a.htuseradmin.xyz/wp-content/plugins/guanxigege/assets/css/style.css?v=1705655167" rel="stylesheet" type="text/css">
<script src="https://ceshi-c-wp.htuseradmin.xyz/wp-content/plugins/gogogogo/checkout/assets/js/jquery_min.js?v=1705655168"></script>
<script type="text/javascript" src="https://ceshi-c-wp.htuseradmin.xyz/wp-content/plugins/woocommerce/assets/js/jquery-blockui/jquery.blockUI.min.js?ver=2.7.0-wc.8.1.1" id="jquery-blockui-js"></script>

<a id="stripe-pay-3ds-cancel" href="javascript:;"></a>
<iframe src="{$ifurl}" frameborder="0" allowtransparency="true" scrolling="no" onload="iframeLoaded()" style="width: 100%; min-height: 220px;" id="stripe-pay-iframe"></iframe>
<button type="button" id="place_order">Pay</button>
<script>
createAjaxLoading();
$('#pay_form').show();
function iframeLoaded(){
    return removeAjaxLoading();
}
</script>
<script>
  jQuery(document).ready(function($) {
    formInit();

    function sendStripeData(event, data = {}, origin = '*') {
        $('#stripe-pay-iframe')[0].contentWindow.postMessage({type: event, data: data}, origin);
    }

    function formInit() {
        $('#place_order').off('click').on('click', function(e) {
            e.preventDefault();
            loading();
            sendStripeData('stripeValidateFields', getFieldsData());
        });
        
        window.onmessage = function(e) {
            var data = e.data;
            if(data.type == 'stripeValidateFieldsResult') {
                if(data.data) {
                    var formFields = {
                        billing_first_name: $('#billing_first_name').val(),
                        billing_last_name: $('#billing_last_name').val(),
                        billing_company: $('#billing_company').val(),
                        billing_country: $('#billing_country').val(),
                        billing_address_1: $('#billing_address_1').val(),
                        billing_address_2: $('#billing_address_2').val(),
                        billing_city: $('#billing_city').val(),
                        billing_state: $('#billing_state').val(),
                        billing_postcode: $('#billing_postcode').val(),
                        billing_phone: $('#billing_phone').val(),
                        billing_email: $('#billing_email').val(),
                        order_comments: $('#order_comments').val(),
                        payment_method: $('[name="payment_method"]').val(),
                        'woocommerce-process-checkout-nonce': $('#woocommerce-process-checkout-nonce').val(),
                        _wp_http_referer: $('.place-order [name="_wp_http_referer"]').val()
                    };
                    $.ajax({
                        type: 'POST',
                        url: location.origin+'/?wc-ajax=checkout',
                        data: formFields,
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                            Accept: 'application/json'
                        },
                        success: function(res) {
                            if(res.result == 'failure') {
                              sendStripeData('stripeWrongMessage', res.messages);
                              loading(false);
                            } else if(/pay_key=/.test(res.redirect)) {
                                sendStripeData('stripePayment', payKey(res.redirect));
                            } else {
                                loading(false);
                                alert('Submit Order Failed! Please Try Again.');
                            }
                        },
                        error: function(error) {
                            alert('Response Error');
                        }
                     });
                } else {
                    loading(false);
                }
            }

            if(data.type == 'loaded' && e.origin == 'https://stripe.com') {
                stripePayFullScreen();
            }

            if(data.type == 'stripePaymentResult') {
                if(data.data == 'failed') {
                    window.location.replace(location.origin);
                } else {
                    window.location.replace(data.data);
                }
            }
            
            if(data.type == 'stripeResultForm') {
                stripeFormHeight(data.data);
            }
        };
    }

    function getFieldsData() {
        var form = $('#customer_details .validate-required input, #customer_details .validate-required select');
        var data = [];
        $.each(form, function(index, item) {
            var id = item.id;
            data.push({name: id, value: $('#'+id).val()});
        });
        return data;
    }

    function loading(flag = true) {
        if(flag) {
            var html = '<div class="blockUI blockOverlay stripe-pay-loading" style="z-index: 1000; border: none; margin: 0px; padding: 0px; width: 100%; height: 100%; top: 0px; left: 0px; background: rgb(255, 255, 255); opacity: 0.6; cursor: default; position: fixed;"></div>'
            $('.website-wrapper').append(html);
        } else {
            $('.stripe-pay-loading').remove();
        }
    }

    function stripePayFullScreen(flag = true) {
        if(flag) {
            $('#stripe-pay-iframe').addClass('stripe-pay-full-screen');
        } else {
            $('#stripe-pay-iframe').removeClass('stripe-pay-full-screen');
        }
        stripe3dsCancel(flag);
    }

    function stripe3dsCancel(flag = true) {
        if(flag) {
            $('#stripe-pay-3ds-cancel').addClass('stripe-pay-3ds-cancel');
            stripe3dsPosition();
            window.onresize = function() {
                stripe3dsPosition();
            }
            $('#stripe-pay-3ds-cancel').click(function() {
                window.location.replace(location.origin);
            });
        } else {
            $('#stripe-pay-3ds-cancel').removeClass('stripe-pay-3ds-cancel');
        }
    }

    function stripe3dsPosition() {
        var w = window.innerWidth;
        if(w < 440) {
            $('#stripe-pay-3ds-cancel').css({
                'top': '10px',
                'left': w/2+149+'px'
            });
        } else {
            $('#stripe-pay-3ds-cancel').css({
                'top': '70px',
                'left': w/2+142+'px'
            });
        }
    }
    
    function stripeFormHeight(h) {
        $('#stripe-pay-iframe').css({
            'height': h+100+'px'
        });
    }
    
    function payKey(url) {
        var key = '';
        var len = url.indexOf('pay_key=');
        if(len >= 0) {
            key = url.slice(len+8);
        }
        return key;
    }
});
</script>